package JUC.lock;

import Thread.设置获取线程名称的方法.MyThread;

import java.util.concurrent.TimeUnit;

//jps -l   jstack    Found 1 deadlock.
public class DeadlockDemo {
    public static void main(String[] args) {
        String lockA="lockA";
        String lockB="lockB";

        new Thread(new Mythread(lockA,lockB),"T1").start();
        new Thread(new Mythread(lockB,lockA),"T2").start();

    }
}

class Mythread implements Runnable{
    private String lockA;
    private String lockB;



    public Mythread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run(){
        synchronized (lockA){
            System.out.println(Thread.currentThread().getName()+lockA+"=>"+lockB);

            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            synchronized (lockB){
                System.out.println(Thread.currentThread().getName()+lockB+"=>"+lockB);
            }
        }
    }
}
